package com.sls.game.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.ServerDisponseException;
import com.sls.core.util.ServerDateAndTime;
import com.sls.game.constant.ExceptionCodeDes;
import com.sls.game.dao.EnLinkProductDAO;
import com.sls.game.dao.GoodsSupplyManageDAO;
import com.sls.game.dao.OhFeiGoodsInfoDAO;
import com.sls.game.dao.enfu.EnfuGoodsDetailDAO;
import com.sls.game.pojo.GoodsSupplyManageInfo;
import com.sls.game.pojo.NutsNetGoodsInfo;
import com.sls.game.pojo.OhFeiGoodsInfo;
import com.sls.game.pojo.ebaimi.EbaimiProduct;
import com.sls.game.pojo.enfu.EnfuGoodsDetailInfo;
import com.sls.game.pojo.enlink.EnLinkProduct;
import com.sls.game.pojo.jieyitong.JieyitongGoodsInfo;
import com.sls.game.pojo.wogreat.WogreatProduct;
import com.sls.game.pojo.xingqitian.XingParvalueInfo;
import com.sls.game.pojo.xinhe.XinHeProduct;
import com.sls.game.pojo.yunen.YunEnProduct;
import com.sls.game.service.WebBatchOperateGoodsService;

@Service("webBatchOperateGoodsService")
public class WebBatchOperateGoodsServiceImpl implements
		WebBatchOperateGoodsService {

	private static Logger log = Logger
			.getLogger(WebBatchOperateGoodsServiceImpl.class);
	@Resource(name = "ohFeiGoodsInfoDAO")
	private OhFeiGoodsInfoDAO ohFeiGoodsInfoDAO;
	@Resource(name = "enLinkProductDAO")
	private EnLinkProductDAO enLinkProductDAO;
	@Resource(name = "goodsSupplyManageDAO")
	private GoodsSupplyManageDAO goodsSupplyDAO;
	private Gson gson = null;
	@Resource(name = "enfuGoodsDetailDAO")
	private EnfuGoodsDetailDAO enfuGoodsDetailDAO;

	public WebBatchOperateGoodsServiceImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public void updateBatchNutsNetGoods(String body, Head head) {
		final NutsNetGoodsInfo info = gson.fromJson(body,
				NutsNetGoodsInfo.class);
		this.validate(info);
		if (StringUtils.isEmpty(info.getOperator())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.OPERATOR_NO_NULL.getCode(),
					ExceptionCodeDes.OPERATOR_NO_NULL.getDescription());
		}
		if (StringUtils.isEmpty(info.getGoodsState())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.GOODS_STATUS_NULL.getCode(),
					ExceptionCodeDes.GOODS_STATUS_NULL.getDescription());
		}
		String modifyTime = ServerDateAndTime.getNowDateTimeOfFormat();
		String operator = info.getOperator();
		String goodsState = info.getGoodsState();
		GoodsSupplyManageInfo supplyInfo = new GoodsSupplyManageInfo();
		supplyInfo.setModifyTime(modifyTime);
		supplyInfo.setOperator(operator);
		supplyInfo.setGoodsState(goodsState);
		supplyInfo.setSuppliersId("20121001");
		try {
			goodsSupplyDAO.updateState2(supplyInfo);
		} catch (Exception e) {
			log.error("批量上下架卡易售产品出现异常", e);
			throw new ServerDisponseException("BATCH_ON_OFF_EXCEPTION",
					"批量上下架出现异常");
		}

	}

	@Override
	public void updateBatchOhFlyGoods(String body, Head head) {
		final OhFeiGoodsInfo info = gson.fromJson(body, OhFeiGoodsInfo.class);
		this.validate(info);
		if (StringUtils.isEmpty(info.getOperator())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.OPERATOR_NO_NULL.getCode(),
					ExceptionCodeDes.OPERATOR_NO_NULL.getDescription());
		}
		if (StringUtils.isEmpty(info.getGoodsState())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.GOODS_STATUS_NULL.getCode(),
					ExceptionCodeDes.GOODS_STATUS_NULL.getDescription());
		}
		// 查询殴飞所有商品
		final List<OhFeiGoodsInfo> infoList = this.ohFeiGoodsInfoDAO
				.queryAllGoodsInfo();
		new Thread(new Runnable() {

			@Override
			public void run() {
				if (infoList != null && !infoList.isEmpty()) {
					for (OhFeiGoodsInfo goodsInfo : infoList) {
						String originalGoodsId = goodsInfo.getGoodsId();
						GoodsSupplyManageInfo supplyInfo = goodsSupplyDAO
								.queryByOriginalGoodsId(originalGoodsId);
						if (supplyInfo != null) {
							String id = supplyInfo.getId();
							String modifyTime = ServerDateAndTime
									.getNowDateTimeOfFormat();
							String operator = info.getOperator();
							String goodsState = info.getGoodsState();
							GoodsSupplyManageInfo supplyInfo2 = new GoodsSupplyManageInfo();
							supplyInfo2.setId(id);
							supplyInfo2.setModifyTime(modifyTime);
							supplyInfo2.setOperator(operator);
							supplyInfo2.setGoodsState(goodsState);
							try {
								goodsSupplyDAO.updateState(supplyInfo2);
							} catch (Exception e) {
								log.error(e);
								continue;
							}
						}

					}
				}

			}

		}).start();
	}

	@Override
	public void updateBatchEnLinkGoods(String body, Head head) {
		EnLinkProduct enlink = gson.fromJson(body, EnLinkProduct.class);
		this.validate(enlink);
		if (StringUtils.isEmpty(enlink.getOperator())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.OPERATOR_NO_NULL.getCode(),
					ExceptionCodeDes.OPERATOR_NO_NULL.getDescription());
		}
		if (StringUtils.isEmpty(enlink.getGoodsState())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.GOODS_STATUS_NULL.getCode(),
					ExceptionCodeDes.GOODS_STATUS_NULL.getDescription());
		}
		// 查询恩联所有商品
		List<String> originalGoodsIdList = this.enLinkProductDAO
				.queryAllProductId();
		if (originalGoodsIdList != null && !originalGoodsIdList.isEmpty()) {
			for (String originalGoodsId : originalGoodsIdList) {
				GoodsSupplyManageInfo supplyInfo = this.goodsSupplyDAO
						.queryByOriginalGoodsId(originalGoodsId);
				if (supplyInfo != null) {
					String id = supplyInfo.getId();
					String modifyTime = ServerDateAndTime
							.getNowDateTimeOfFormat();
					String operator = enlink.getOperator();
					String goodsState = enlink.getGoodsState();
					GoodsSupplyManageInfo supplyInfo2 = new GoodsSupplyManageInfo();
					supplyInfo2.setId(id);
					supplyInfo2.setModifyTime(modifyTime);
					supplyInfo2.setOperator(operator);
					supplyInfo2.setGoodsState(goodsState);
					try {
						this.goodsSupplyDAO.updateState(supplyInfo2);
					} catch (Exception e) {
						log.error(e);
						continue;
					}
				}

			}
		}
	}

	@Override
	public void updateBatchXQTGoods(String body, Head head) {
		XingParvalueInfo parInfo = gson.fromJson(body,
				XingParvalueInfo.class);
		this.validate(parInfo);
		if (StringUtils.isEmpty(parInfo.getOperator())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.OPERATOR_NO_NULL.getCode(),
					ExceptionCodeDes.OPERATOR_NO_NULL.getDescription());
		}
		if (StringUtils.isEmpty(parInfo.getGoodsState())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.GOODS_STATUS_NULL.getCode(),
					ExceptionCodeDes.GOODS_STATUS_NULL.getDescription());
		}
		String modifyTime = ServerDateAndTime.getNowDateTimeOfFormat();
		String operator = parInfo.getOperator();
		String goodsState = parInfo.getGoodsState();
		GoodsSupplyManageInfo supplyInfo = new GoodsSupplyManageInfo();
		supplyInfo.setModifyTime(modifyTime);
		supplyInfo.setOperator(operator);
		supplyInfo.setGoodsState(goodsState);
		supplyInfo.setSuppliersId("20121004");
		try {
			goodsSupplyDAO.updateState2(supplyInfo);
		} catch (Exception e) {
			log.error("批量上下架星启天产品出现异常", e);
			throw new ServerDisponseException("BATCH_ON_OFF_EXCEPTION",
					"批量上下架出现异常");
		}

	}

	@Override
	public void updateBatchJpayGoods(String body, Head head) {
		JieyitongGoodsInfo jInfo = gson.fromJson(body,
				JieyitongGoodsInfo.class);
		this.validate(jInfo);
		if (StringUtils.isEmpty(jInfo.getOperator())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.OPERATOR_NO_NULL.getCode(),
					ExceptionCodeDes.OPERATOR_NO_NULL.getDescription());
		}
		if (StringUtils.isEmpty(jInfo.getStatus())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.GOODS_STATUS_NULL.getCode(),
					ExceptionCodeDes.GOODS_STATUS_NULL.getDescription());
		}
		String operator = jInfo.getOperator();
		String goodsState = jInfo.getStatus();
		String modifyTime = ServerDateAndTime.getNowDateTimeOfFormat();
		GoodsSupplyManageInfo supplyInfo = new GoodsSupplyManageInfo();
		supplyInfo.setModifyTime(modifyTime);
		supplyInfo.setOperator(operator);
		supplyInfo.setGoodsState(goodsState);
		supplyInfo.setSuppliersId("20121005");
		try {
			goodsSupplyDAO.updateState2(supplyInfo);
		} catch (Exception e) {
			log.error("批量上下架捷易通产品出现异常", e);
			throw new ServerDisponseException("BATCH_ON_OFF_EXCEPTION",
					"批量上下架出现异常");
		}
	}

	@Override
	public void updateBatchEBaiMiGoods(String body, Head head) {
		EbaimiProduct eInfo = gson.fromJson(body, EbaimiProduct.class);
		this.validate(eInfo);
		if (StringUtils.isEmpty(eInfo.getOperator())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.OPERATOR_NO_NULL.getCode(),
					ExceptionCodeDes.OPERATOR_NO_NULL.getDescription());
		}
		if (StringUtils.isEmpty(eInfo.getStatus())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.GOODS_STATUS_NULL.getCode(),
					ExceptionCodeDes.GOODS_STATUS_NULL.getDescription());
		}
		String modifyTime = ServerDateAndTime.getNowDateTimeOfFormat();
		String operator = eInfo.getOperator();
		String goodsState = eInfo.getStatus();
		GoodsSupplyManageInfo supplyInfo = new GoodsSupplyManageInfo();
		supplyInfo.setModifyTime(modifyTime);
		supplyInfo.setOperator(operator);
		supplyInfo.setGoodsState(goodsState);
		supplyInfo.setSuppliersId("20121006");
		try {
			goodsSupplyDAO.updateState2(supplyInfo);
		} catch (Exception e) {
			log.error("批量上下架易百米产品出现异常", e);
			throw new ServerDisponseException("BATCH_ON_OFF_EXCEPTION",
					"批量上下架出现异常");
		}
	}

	@Override
	public void updateBatchWogreatGoods(String body, Head head) {
		WogreatProduct wInfo = gson.fromJson(body, WogreatProduct.class);
		this.validate(wInfo);
		if (StringUtils.isEmpty(wInfo.getOperator())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.OPERATOR_NO_NULL.getCode(),
					ExceptionCodeDes.OPERATOR_NO_NULL.getDescription());
		}
		if (StringUtils.isEmpty(wInfo.getStatus())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.GOODS_STATUS_NULL.getCode(),
					ExceptionCodeDes.GOODS_STATUS_NULL.getDescription());
		}
		String modifyTime = ServerDateAndTime.getNowDateTimeOfFormat();
		String operator = wInfo.getOperator();
		String goodsState = wInfo.getStatus();
		GoodsSupplyManageInfo supplyInfo = new GoodsSupplyManageInfo();
		supplyInfo.setModifyTime(modifyTime);
		supplyInfo.setOperator(operator);
		supplyInfo.setGoodsState(goodsState);
		supplyInfo.setSuppliersId("20121008");
		try {
			goodsSupplyDAO.updateState2(supplyInfo);
		} catch (Exception e) {
			log.error("批量上下架沃谷产品出现异常", e);
			throw new ServerDisponseException("BATCH_ON_OFF_EXCEPTION",
					"批量上下架出现异常");
		}
	}

	@Override
	public void updateBatchYunGoods(String body, Head head) {
		YunEnProduct yInfo = gson.fromJson(body, YunEnProduct.class);
		this.validate(yInfo);
		if (StringUtils.isEmpty(yInfo.getOperator())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.OPERATOR_NO_NULL.getCode(),
					ExceptionCodeDes.OPERATOR_NO_NULL.getDescription());
		}
		if (StringUtils.isEmpty(yInfo.getStatus())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.GOODS_STATUS_NULL.getCode(),
					ExceptionCodeDes.GOODS_STATUS_NULL.getDescription());
		}
		String modifyTime = ServerDateAndTime.getNowDateTimeOfFormat();
		String operator = yInfo.getOperator();
		String goodsState = yInfo.getStatus();
		GoodsSupplyManageInfo supplyInfo2 = new GoodsSupplyManageInfo();
		supplyInfo2.setModifyTime(modifyTime);
		supplyInfo2.setOperator(operator);
		supplyInfo2.setGoodsState(goodsState);
		supplyInfo2.setSuppliersId("20121009");
		try {
			goodsSupplyDAO.updateState2(supplyInfo2);
		} catch (Exception e) {
			log.error("批量上下架云恩产品出现异常", e);
			throw new ServerDisponseException("BATCH_ON_OFF_EXCEPTION",
					"批量上下架出现异常");
		}
	}

	/**
	 * 校验是否为空
	 * 
	 * @param obj
	 */
	public void validate(Object obj) {
		if (obj == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.PARSE_REQUEST_ERROR.getCode(),
					ExceptionCodeDes.PARSE_REQUEST_ERROR.getDescription());
		}
	}

	/**
	 * 批量操作 恩付商品上下架
	 * 
	 * @param body
	 * @param head
	 */
	@Override
	public void updateBatchOfEnfu(String body, Head head) {
		EnfuGoodsDetailInfo detailInfo = gson.fromJson(body,
				EnfuGoodsDetailInfo.class);

		this.validate(detailInfo);
		boolean flag = checkDataOfEnfu(detailInfo);
		if (flag) {
			// 查询恩付所有商品
			List<String> definedproductidList = enfuGoodsDetailDAO
					.queryAllDefinedproductid();
			if (definedproductidList != null && definedproductidList.size() > 0) {
				for (int i = 0; i < definedproductidList.size(); i++) {
					String originalGoodsId = definedproductidList.get(i);
					GoodsSupplyManageInfo supplyInfo = this.goodsSupplyDAO
							.queryByOriginalGoodsId(originalGoodsId);
					if (supplyInfo != null) {
						String id = supplyInfo.getId();
						String modifyTime = ServerDateAndTime
								.getNowDateTimeOfFormat();
						String operator = detailInfo.getOperator();
						String goodsState = detailInfo.getGoodsState();
						GoodsSupplyManageInfo supplyInfo2 = new GoodsSupplyManageInfo();
						supplyInfo2.setId(id);
						supplyInfo2.setModifyTime(modifyTime);
						supplyInfo2.setOperator(operator);
						supplyInfo2.setGoodsState(goodsState);
						try {
							this.goodsSupplyDAO.updateState(supplyInfo2);
						} catch (Exception e) {
							log.error(e);
							continue;
						}
					}

				}
			}
		}
	}

	/**
	 * 
	 * @param detailInfo
	 * @return
	 */
	private boolean checkDataOfEnfu(EnfuGoodsDetailInfo detailInfo) {
		boolean flag = false;
		if (StringUtils.isEmpty(detailInfo.getOperator())) {
			flag = false;
			throw new ServerDisponseException(
					ExceptionCodeDes.OPERATOR_NO_NULL.getCode(),
					ExceptionCodeDes.OPERATOR_NO_NULL.getDescription());
		} else if (StringUtils.isEmpty(detailInfo.getGoodsState())) {
			flag = false;
			throw new ServerDisponseException(
					ExceptionCodeDes.GOODS_STATUS_NULL.getCode(),
					ExceptionCodeDes.GOODS_STATUS_NULL.getDescription());
		} else {
			flag = true;
		}
		return flag;
	}


	@Override
	public void updateBatchOfXinHeGoods(String body, Head head) {
		XinHeProduct xInfo = gson.fromJson(body, XinHeProduct.class);
		this.validate(xInfo);
		if (StringUtils.isEmpty(xInfo.getOperator())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.OPERATOR_NO_NULL.getCode(),
					ExceptionCodeDes.OPERATOR_NO_NULL.getDescription());
		}
		if (StringUtils.isEmpty(xInfo.getStatus())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.GOODS_STATUS_NULL.getCode(),
					ExceptionCodeDes.GOODS_STATUS_NULL.getDescription());
		}
		String modifyTime = ServerDateAndTime.getNowDateTimeOfFormat();
		String operator = xInfo.getOperator();
		String goodsState = xInfo.getStatus();
		GoodsSupplyManageInfo supplyInfo2 = new GoodsSupplyManageInfo();
		supplyInfo2.setModifyTime(modifyTime);
		supplyInfo2.setOperator(operator);
		supplyInfo2.setGoodsState(goodsState);
		supplyInfo2.setSuppliersId("20121011");
		try {
			goodsSupplyDAO.updateState2(supplyInfo2);
		} catch (Exception e) {
			log.error("批量上下架鑫合产品出现异常", e);
			throw new ServerDisponseException("BATCH_ON_OFF_EXCEPTION",
					"批量上下架出现异常");
		}
		
	}
	/**
	 * 根据供应商号，进行批量上下架
	 * */
	public void updateBatchbySuppliersId(String body, Head head){
		GoodsSupplyManageInfo xInfo = gson.fromJson(body, GoodsSupplyManageInfo.class);
		this.validate(xInfo);
		String suppliersId = xInfo.getSuppliersId();
		
		if (StringUtils.isEmpty(xInfo.getOperator())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.OPERATOR_NO_NULL.getCode(),
					ExceptionCodeDes.OPERATOR_NO_NULL.getDescription());
		}
		if (StringUtils.isEmpty(xInfo.getGoodsState())) {
			throw new ServerDisponseException(
					ExceptionCodeDes.GOODS_STATUS_NULL.getCode(),
					ExceptionCodeDes.GOODS_STATUS_NULL.getDescription());
		}
		if(StringUtils.isEmpty(suppliersId)){
			throw new ServerDisponseException(
					ExceptionCodeDes.SUPPLY_ID_NULL.getCode(),
					ExceptionCodeDes.SUPPLY_ID_NULL.getDescription());
		}
		
		String modifyTime = ServerDateAndTime.getNowDateTimeOfFormat();
		String operator = xInfo.getOperator();
		String goodsState = xInfo.getGoodsState();
		
		GoodsSupplyManageInfo supplyInfo2 = new GoodsSupplyManageInfo();
		supplyInfo2.setModifyTime(modifyTime);
		supplyInfo2.setOperator(operator);
		supplyInfo2.setGoodsState(goodsState);
		supplyInfo2.setSuppliersId(suppliersId);
		try {
			goodsSupplyDAO.updateState2(supplyInfo2);
		} catch (Exception e) {
			log.error("批量上下架出现异常"+head.getRequestId(), e );
			throw new ServerDisponseException("BATCH_ON_OFF_EXCEPTION",
					"批量上下架出现异常");
		}
	}

}
